package com.huishu.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huishu.commons.pojo.Result;
import com.huishu.commons.utlis.BeanUtils;
import com.huishu.goods.mapper.ProductItemMapper;
import com.huishu.goods.pojo.model.ProductItemDO;
import com.huishu.goods.pojo.vo.CartItemInfoVO;
import com.huishu.goods.service.ProductItemService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author generator@ljj
 * @since 2023-05-29
 */
@Service
public class ProductItemServiceImpl extends ServiceImpl<ProductItemMapper, ProductItemDO> implements ProductItemService {

    @Override
    public Result findItemInfo(Long id) {
        ProductItemDO productItemDO = getById(id);
        Long productId = productItemDO.getProductId();
        LambdaQueryWrapper<ProductItemDO> queryWrapper = Wrappers.lambdaQuery(ProductItemDO.class)
                .eq(ProductItemDO::getProductId, productId);
        List<ProductItemDO> productItems = this.list(queryWrapper);
        List<CartItemInfoVO> collect = productItems.stream().map(p -> {
            CartItemInfoVO cartItemInfoVO = BeanUtils.copyProperties(p, CartItemInfoVO.class);
            if (p.getId().equals(id)) {
                cartItemInfoVO.setIsSelect(true);
            }
            return cartItemInfoVO;
        }).collect(Collectors.toList());
        return Result.success(collect);
    }
}
